package cn.my.android_demo;

import org.pjsip.pjsua2.*;

// Subclass to extend the Account and get notifications etc.
class MyAccount extends Account {
    @Override
    public void onRegState(OnRegStateParam prm) {
        System.out.println("*** On registration state: " + prm.getCode() + prm.getReason());
    }
}

public class pjsua2 {
    static {
        System.loadLibrary("pjsua2");
        System.out.println("Library loaded");
    }

    public static void main(String argv[]) {
        try {
            // Create endpoint
            Endpoint ep = new Endpoint();
            ep.libCreate();
            // Initialize endpoint
            EpConfig epConfig = new EpConfig();
            ep.libInit( epConfig );
            // Create SIP transport. Error handling sample is shown
            TransportConfig sipTpConfig = new TransportConfig();
            sipTpConfig.setPort(5060);
            ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_UDP, sipTpConfig);
            // 启动库
            ep.libStart();

            AccountConfig acfg = new AccountConfig();
            acfg.setIdUri("sip:test@sip.pjsip.org");
            acfg.getRegConfig().setRegistrarUri("sip:sip.pjsip.org");
            AuthCredInfo cred = new AuthCredInfo("digest", "*", "test", 0, "secret");
            acfg.getSipConfig().getAuthCreds().add( cred );
            // 创建帐户
            MyAccount acc = new MyAccount();
            acc.create(acfg);
            // 在这里我们没有其他事情要做..
            Thread.sleep(10000);
            /* Explicitly 删除该帐户。
             * 这是为了避免 GC 在删除账户之前先删除 Endpoint。
             */
            acc.delete();

            // Explicitly 销毁和删除终端节点
            ep.libDestroy();
            ep.delete();

        } catch (Exception e) {
            System.out.println(e);
            return;
        }
    }
}
